using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;

namespace HeroldIT.Dnn.Modules.News.Views
{
    public class NewsDetailSettings : SettingsBase, INewsDetailSettingsProvider
    {
        #region control fields

        // TODO: Add control fields here.
        protected ListControl lstListTabId;

        #endregion

        #region properties

        protected override string SkinUrl
        {
            get { return this.GetDetailSkinUrl(); }
            set { this.SetDetailSkinUrl(value); }
        }

        #endregion

        #region page lifecycle

        protected override void OnInit(EventArgs e)
        {
            var tabs = Globals.GetPortalTabs(this.PortalSettings.DesktopTabs, false, true, false, false).Cast<TabInfo>().Where(tab => PortalSecurity.IsInRoles(tab.AuthorizedRoles));
            if (null != this.lstListTabId)
            {
                this.lstListTabId.DataSource = tabs;
                this.lstListTabId.DataTextField = "TabName";
                this.lstListTabId.DataValueField = "TabID";
                this.lstListTabId.DataBind();
                this.lstListTabId.Items.Insert(0, new ListItem(this.GetString("liUseDefault.Text", "<Use default>"), "-1"));
            }
            base.OnInit(e);
        }

        #endregion

        #region methods

        /// <summary>
        /// Loads the module's settings into the given controls.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public override void LoadSettings()
        {
            base.LoadSettings();

            try
            {
                if (!this.Page.IsPostBack)
                {
                    // TODO: Fill control values from settings values here.
                    if (null != this.lstListTabId)
                    {
                        this.lstListTabId.SelectedValue = this.GetListTabId().ToString(CultureInfo.InvariantCulture);
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// Updates the module's settings from the given controls.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public override void UpdateSettings()
        {
            try
            {
                // TODO: Set settings from control values here.
                if (null != this.lstListTabId)
                {
                    int tabId;
                    if (int.TryParse(this.lstListTabId.SelectedValue, NumberStyles.None, CultureInfo.InvariantCulture, out tabId))
                    {
                        this.SetListTabId(tabId);
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

            base.UpdateSettings();
        }

        #endregion
    }
}